module Fighter {
	/**
	 * 战斗场景显示容器 ---刘伟
	 * */
	export class AirBattle extends egret.DisplayObjectContainer {
		constructor() {
			super();
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
			this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeFromStage, this);

			//制造敌机计时器，初始化计时器
			this.timerCreateFly = new egret.Timer(600, 0);
			this.timerCreateFly.addEventListener(egret.TimerEvent.TIMER, this.createEnemy, this);
		}
		/**是否已经一波结束 */
		public turnOver: boolean = false;
		/**制造飞机计时器 */
		public timerCreateFly: egret.Timer;
		/**正在制造飞机 */
		private isCreatingFly: boolean;
		/**联网模式开始制造飞机 */
		public static startCreatePlane = false;
		/**制造飞机序号 */
		public indexFly: number = 0;
		/**容器名字 */
		public name = "AirBattle";
		/**飞机管理层 */
		public flyManager: Fighter.FlyManager;
		/**玩家控制飞机 */
		public player: Fighter.PlayerControl;
		/**上一帧时间点 */
		public static lastTimer: number;


		/**子弹管理层*/
		public BulletManager: Bullet.BulletManager;
		/**动画管理层*/
		public Animation: Animation.AnimationManager;
		/**环绕僚机 */
		public aroundPlane: AroundFly;
		/**跟随飞机 */
		public followPlane: FollowFly;
		/**游戏1加入到舞台 */
		private addToStage(evt: egret.Event) {
			if (this.Animation == null) {
				this.Animation = new Animation.AnimationManager(this);
			}
			if (this.flyManager == null) {
				this.flyManager = new Fighter.FlyManager(this);
			}

			if (this.player == null) {

				this.player = new PlayerControl(this);


				this.aroundPlane = new AroundFly(this);
				this.followPlane = new FollowFly(this);

			}
			if (this.BulletManager == null) {
				this.BulletManager = new Bullet.BulletManager(this);
			}


			this.player.init(this.BulletManager);
			//如果购买了环绕僚机
			if (GameUtil.PlayerMessage.instance.isBuyAroundPlane == true) {
				this.aroundPlane.init(this.player.planeSprit);

			}
			//如果购买了跟随敌机
			if (GameUtil.PlayerMessage.instance.isBuyFollowPlane == true) {
				this.followPlane.init(this.player.planeSprit);
			}


			AirBattle.lastTimer = egret.getTimer();
			if (GameUtil.PlayerMessage.instance.onePerson) {
				this.timerCreateFly.start();
			}
			this.isCreatingFly = true;

			this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
		}
		/**游戏1离开舞台 */
		private removeFromStage(evt: egret.Event) {

		}

		/**
		 * 每帧监听
		 * */
		public update(evt: egret.Event) {
			//计算每帧间隔时间
			let now = egret.getTimer();
			//	var fps= 1000/(now - AirBattle. lastTimer);

			let deltaTime = now - AirBattle.lastTimer;
			AirBattle.lastTimer = now;
			//每帧运行

			if (!GameUtil.PlayerMessage.instance.onePerson && AirBattle.startCreatePlane) {
				AirBattle.startCreatePlane = false;
				this.timerCreateFly.start();
			}

			this.flyManager.update(deltaTime);
			this.BulletManager.update(deltaTime);
			//console.log(deltaTime);

			if (this.player.isLiving) {
				this.player.update();
				if (GameUtil.PlayerMessage.instance.isBuyFollowPlane == true) {
					this.followPlane.follow();
				}
				if (GameUtil.PlayerMessage.instance.isBuyAroundPlane == true) {
					this.aroundPlane.around();
					this.aroundPlane.hittedByBullet();
				}

			}

			if (!this.player.isLiving) {
				if (GameUtil.PlayerMessage.instance.isBuyAroundPlane == true) {
					GameUtil.PlayerMessage.instance.isBuyAroundPlane = false;

					this.aroundPlane.removeSelf();

				}
				if (GameUtil.PlayerMessage.instance.isBuyFollowPlane == true) {
					GameUtil.PlayerMessage.instance.isBuyFollowPlane = false;
					this.followPlane.removeSelf();

				}
			}

			if (!this.isCreatingFly) {
				this.isTurnEnd();
			}
			//记录此帧时间点
			//AirBattle. lastTimer = egret.getTimer();
		}

		/**创建飞机 */
		private createEnemy(evt: egret.TimerEvent) {
			let type: any;
			//同时放出两架飞机
			type = GameUtil.PlayerMessage.instance.getEnemy[this.indexFly];
			let point = GameUtil.PlayerMessage.instance.airBattlePosition[this.indexFly];
			if (!GameUtil.PlayerMessage.instance.onePerson) {
				point = new egret.Point(point.positionX, point.positionY);
			}
			//console.log(type);
			this.flyManager.createFly(type, point);
			this.indexFly++;
			if (this.indexFly == GameUtil.PlayerMessage.instance.getEnemy.length) {
				this.timerCreateFly.reset();
				this.isCreatingFly = false;
				this.indexFly = 0;
			}

			type = GameUtil.PlayerMessage.instance.getEnemy[this.indexFly];
			let point2 = GameUtil.PlayerMessage.instance.airBattlePosition[this.indexFly];
			if (!GameUtil.PlayerMessage.instance.onePerson) {
				point2 = new egret.Point(point.positionX, point.positionY);
			}
			//console.log(type);
			while (egret.Point.distance(point, point2) < 50) {
				point2.x = Math.random() * 500;
				point2.y = Math.random() * 100;
			}
			this.flyManager.createFly(type, point2);

			this.indexFly++;
			if (this.indexFly == GameUtil.PlayerMessage.instance.getEnemy.length) {
				this.timerCreateFly.reset();
				this.isCreatingFly = false;
				this.indexFly = 0;
			}
		}


		/**判断游戏场景1结束 */
		private isTurnEnd() {

			for (let i = 0; i < this.flyManager.productflys.length; i++) {
				let fly = this.flyManager.productflys[i];
				if (fly.isLiving) {
					return;
				}
				if (i == this.flyManager.productflys.length - 1) {
					this.flyManager.fightRoundEnd();
				}
			}
			if (this.player.isLiving && this.flyManager.productflys.length == 0) {

				this.BulletManager.fightRoundEnd();
				this.player.detrory();
				if (GameUtil.PlayerMessage.instance.isBuyAroundPlane == true) {
					this.aroundPlane.removeSelf();
				}
				if (GameUtil.PlayerMessage.instance.isBuyFollowPlane == true) {
					this.followPlane.removeSelf();
				}


			}
			if (!this.player.isLiving && this.flyManager.productflys.length == 0) {
				this.BulletManager.fightRoundEnd();
				this.player.detrory();
				if (GameUtil.PlayerMessage.instance.isBuyAroundPlane == true) {
					this.aroundPlane.removeSelf();
				}
				if (GameUtil.PlayerMessage.instance.isBuyFollowPlane == true) {
					this.followPlane.removeSelf();
				}
			}

			if (this.BulletManager.productBulletus.length == 0 && this.flyManager.productflys.length == 0) {
				this.removeEventListener(egret.Event.ENTER_FRAME, this.update, this);
				let TimeEnd: egret.Timer = new egret.Timer(1200, 1);
				TimeEnd.addEventListener(egret.TimerEvent.TIMER, this.IsturnOver, this);
				TimeEnd.start();
			}
		}
		/**延时跳转*/
		public IsturnOver() {
			this.turnOver = true;
		}
	}
}